package com.zzty.servlet;

import com.zzty.common.LoginEmailSend;
import com.zzty.daoimpl.UserDaoImpl;
import com.zzty.entity.Clazz;
import com.zzty.entity.User;
import com.zzty.service.BoolUserService;
import com.zzty.serviceimpl.BoolUserServiceImpl;
import org.apache.taglibs.standard.lang.jstl.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "/BoolServlet", urlPatterns = "/BoolServlet")
public class BoolServlet extends javax.servlet.http.HttpServlet {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BoolServlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        logger.info("method:{},{}", "method", method);
        if ("boolUser".equals(method)) {
            try {
                boolUser(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("boolUserSecret".equals(method)) {
            try {
                boolUserSecret(request, response);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

        if ("searchClazz".equals(method)) {
            try {
                searchClazz(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("changePassword".equals(method)) {
            try {
                changePassword(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("searchUser".equals(method)) {
            try {
                searchUser(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("sendEmail".equals(method)) {
            try {
                sendEmail(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void sendEmail(HttpServletRequest request, HttpServletResponse response) {
        String newEmail = request.getParameter("newEmail");
        System.out.println("newEmail "+newEmail);
        LoginEmailSend loginEmailSend = new LoginEmailSend();
        String result = loginEmailSend.sendEmail(newEmail);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(result);
            PrintWriter out = response.getWriter();
            out.write(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void boolUser(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        BoolUserService bd = new BoolUserServiceImpl();
        User user = new User();
        String name = request.getParameter("name");
        user.setName(name);
        String password = request.getParameter("password");
        user.setPassword(password);
        Integer clazzId = Integer.valueOf(request.getParameter("clazzId"));
        user.setClassId(clazzId);
        String email = request.getParameter("email");
        user.setEmail(email);
        String type = request.getParameter("type");
        user.setType(type);
        String result = bd.boolUser(user);
        System.out.println("test result "+result);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void boolUserSecret(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        BoolUserService bd = new BoolUserServiceImpl();
        User user = new User();
        String name = request.getParameter("name");
        user.setName(name);
        String password = request.getParameter("password");
        user.setPassword(password);
        Integer id = bd.searchId(user);
        System.out.println("id="+id);
        user.setId(id);
        User users = bd.searchUser(id);
        System.out.println("name="+name+"password="+password);
        HttpSession session = request.getSession();
        session.setAttribute("user", users);
        logger.info("name:{}", name);
        logger.info("password:{}", password);
        String result = bd.boolUserSecret(user);
        System.out.println("result="+result);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void searchClazz(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        UserDaoImpl ud = new UserDaoImpl();
        List<Clazz> clazz = ud.searchClazz();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(clazz);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void changePassword(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        BoolUserService bd = new BoolUserServiceImpl();
        User users = (User) request.getSession().getAttribute("user");
        String password = request.getParameter("password");
        String email=request.getParameter("email");
        String name=request.getParameter("name");
        if(password==null||password.isEmpty()){
            password=users.getPassword();
        }
        if(email==null||email.isEmpty()){
            email=users.getEmail();
        }
        if(name==null||name.isEmpty()){
            name=users.getName();
        }
        Integer id = users.getId();
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        user.setEmail(email);
        user.setName(name);
        String result = bd.changePassword(user);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void searchUser(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        BoolUserService bd = new BoolUserServiceImpl();
        Integer id = Integer.valueOf(request.getParameter("id"));
        User user = bd.searchUser(id);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
}
